﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogre;

namespace WastedMechanics
{
    class TypedInput
    {
        public int KeysCount;
        public bool[] IsKeyTyped;
        float[] KeyDownTime;

        public TypedInput()
        {
            KeysCount = (int)(Enum.GetValues(typeof(MOIS.KeyCode)) as MOIS.KeyCode[]).Max()+1;
            IsKeyTyped = new bool[KeysCount];
            KeyDownTime = new float[KeysCount];
        }
        
        public void Update()
        {
            for (int keyCode = 0; keyCode < KeysCount; keyCode++)
            {
                IsKeyTyped[keyCode] = false;

                if (Engine.Keyboard.IsKeyDown((MOIS.KeyCode)keyCode))
                {                    
                    float oldKeyDownTime = KeyDownTime[keyCode];
                    KeyDownTime[keyCode] += Engine.TimeStep;

                    if ((oldKeyDownTime == 0.0f) ||
                        ((oldKeyDownTime < 0.7f) && (KeyDownTime[keyCode] >= 0.7f)) ||
                        ((KeyDownTime[keyCode] > 0.7f) && (oldKeyDownTime % 0.1f > KeyDownTime[keyCode] % 0.1f)))
                    {
                        IsKeyTyped[keyCode] = true;                        
                    }
                }
                else                                    
                    KeyDownTime[keyCode] = 0;                
            }
        }

        public void ClearTypedKeys()
        {
            for (int keyCode = 0; keyCode < KeysCount; keyCode++)
                IsKeyTyped[keyCode] = false;
        }
    }
}
